package com.mengxuegu.springboot.controller;

import com.alibaba.fastjson.JSON;
import com.mengxuegu.springboot.entities.Result;
import com.mengxuegu.springboot.entities.UsersBean;
import com.mengxuegu.springboot.mapper.BankUserMapper;
import com.mengxuegu.springboot.util.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;


//@RequestMapping("/user")
//@RestController
@Controller
public class BankUserController {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    BankUserMapper bankUserMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Value("${userlogintimeout}")
    private long  userlogintimeout;


    @PostMapping("/getByUserIname")
    public Object getByUserIname(@RequestBody UsersBean bean){
        logger.info("+++++"+bean.getIname());
        return bankUserMapper.getByUserIname(bean.getIname());
    }


    @PostMapping("/regUser")
    public boolean addUser(@RequestBody UsersBean usersBean) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        //1 先判断账号、身份证号是否重复

        String md5 = MD5Util.getMD5(usersBean.getPwd());
        usersBean.setPwd(md5);
        int i = bankUserMapper.addUser(usersBean);
        if(i > 0){
            return true;
        }
        return false;
    }


    @PostMapping("/login")
//    public Object userLogin(@RequestBody UsersBean bean) throws UnsupportedEncodingException, NoSuchAlgorithmException {
//
//        logger.info("+++++"+bean.getIname());
//        Result result = new Result();
//        //先判断
//        if(StringUtils.isEmpty(bean.getIname())|| StringUtils.isEmpty(bean.getPwd())){
//            result.setCode("0");
//            result.setMessage("账号或者密码为空！");
//            return result;
//        }

    public Object userLogin(String username,String password) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        logger.info("+++++"+username);
        Result result = new Result();

        //先判断
        if(StringUtils.isEmpty(username)|| StringUtils.isEmpty(password)){
            result.setCode("0");
            result.setMessage("账号或者密码为空！");
//            return result;

        }else {

            //根据账号查询用户信息
            UsersBean user = bankUserMapper.getByUserIname(username);
            if (null == user) {
                result.setCode("0");
                result.setMessage("账号不存在！");
            } else {
                String md5 = MD5Util.getMD5(user.getPwd());
                logger.info("密码" + password + "穿参加密后：" + md5);

                if (md5.equals(user.getPwd())) {
                    //保存redis

                    logger.info("aaa" + JSON.toJSONString(user));
                    redisTemplate.opsForValue().set(user.getIname(), JSON.toJSONString(user));
                    redisTemplate.expire(user.getIname(), userlogintimeout, TimeUnit.SECONDS);// 设置验证的失效时间
                    result.setCode("1");
                    result.setMessage("登陆成功！");
                    Object test = result;
                    if(((Result) test).getCode().equals("1")){
                        return "redirect:/main.html";
                    }



                } else {
                    result.setCode("0");
                    result.setMessage("密码不一致！！");

                }


            }

        }

        return result;



    }

    //查询用户登陆信息（从redis里面查询）
    @PostMapping("/getLoginUserInfo")
    public Object getLoginUserInfo(@RequestBody UsersBean bean){
        Result result = new Result();
        logger.info("+++++"+bean.getIname());
        Object o = redisTemplate.opsForValue().get(bean.getIname());
        logger.info("++++++"+o);
        if(null==o){
            result.setCode("0");
            result.setMessage("登陆超时");
        }
        else { //返回登陆用户信息
            UsersBean usersBean = JSON.parseObject(o.toString(), UsersBean.class);
            logger.info("++++"+usersBean);
            result.setCode("1");
            result.setData(usersBean);
        }
        return result;
    }


}
